package com.acme.bankapp;
/**
 * Created: Nov 8, 2013 5:28:47 PM
 */

/**
 * @author Alekov Andrey
 *
 */
import java.io.IOException;
import java.util.Properties;

import javax.security.auth.login.AccountNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.acme.bankapp.account.AbstractAccount;
import com.acme.bankapp.account.impl.CheckingAccount;
import com.acme.bankapp.account.impl.SavingAccount;
import com.acme.bankapp.domain.bank.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.BankException;
import com.acme.bankapp.domain.bank.client.Client;
import com.acme.bankapp.domain.bank.client.ClientExistsException;
import com.acme.bankapp.domain.bank.client.Gender;
import com.acme.bankapp.service.bank.BankService;

public class BankApplication {
	final static Logger logger = LoggerFactory.getLogger(BankApplication.class);

	/**
	 * Bank application entry point.
	 */
	public static void main(String[] args) {
		
		BankService bankService = new BankService();
		Properties arguments = new Properties();

		logger.debug("Start Bank Application");
		
		for (int i = 0; i < args.length; i +=2) {
			arguments.put(args[i], args[i+1]);
		}
		logger.debug("Application properties: " + arguments.toString());
		
		Bank myBank = new Bank("Universe Bank");
		
		if (arguments.getProperty("-loadbank") != null){
			try {
				myBank = bankService.readBank(arguments.getProperty("-loadbank"), true);
			} catch (ClassNotFoundException | IOException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			}
		}

		// Test clients
		Client client1 = new Client("Jonh Smith", Gender.MALE);
		Client client2 = new Client("Anna Karenina", Gender.FEMALE);
		
		Account account1 = new CheckingAccount(100, 10, 1L);
		Account account2 = new SavingAccount(100, 2L);
		
		client1.addAccount((AbstractAccount)account1);
		client2.addAccount((AbstractAccount)account2);
		
		try {
			bankService.addClient(myBank, client1);
			bankService.addClient(myBank, client2);
		} catch (ClientExistsException clientException) {
			logger.error(clientException.getMessage());
			clientException.printStackTrace();
		}
		bankService.printBalance(myBank);
		
		try {
			bankService.modifyBank(myBank, client1, 1L,-1000.0);
		} catch (BankException | AccountNotFoundException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			bankService.modifyBank(myBank, client2, 2L, -1000.0);
		} catch (AccountNotFoundException | BankException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
}
